package com.smartlifev30.baiwei.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.smartlifev30.R;

/**
 * 安防 指示器小三角形
 *
 * @author tys
 * @date 2018/8/20
 */

public class Indicator extends View {

    private Paint mPaint;
    private int currentWidth;
    private int currentHeight;
    private Path path;

    public Indicator(Context context) {
        this(context, null);
    }

    public Indicator(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Indicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(getResources().getColor(R.color.stroke));
        path = new Path();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        currentWidth = w;
        currentHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        path.moveTo(currentWidth / 2, 0);
        path.lineTo(0, currentHeight);
        path.lineTo(currentWidth, currentHeight);
        path.close();
        canvas.drawPath(path, mPaint);
    }

    public void smoothToX(int px) {
        ViewGroup.LayoutParams lp = getLayoutParams();
        if (lp instanceof ViewGroup.MarginLayoutParams) {
            ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) lp;
            layoutParams.setMargins(px, 0, 0, 0);
            requestLayout();
        }
    }
}
